package com.open.free.generator.utils;



import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;

/**
 * @author Faye.Wang
 * @version 1.0
 * @date 2020/12/17 18:48
 * @Description
 */
@Component
public class FreemarkerUtils {

    @Autowired
    private  FreeMarkerConfigurer freeMarkerConfigurer;
    private static final String PATH = "/templates/" ;
    private  Template getTemplate(String templateFile) throws IOException {
        Template template = freeMarkerConfigurer.getConfiguration().getTemplate(templateFile);
        return template;
    }

    public String processTemplateIntoString(String templateFile, Map<String, Object> dataMap) throws IOException, TemplateException {
        Template template = getTemplate(templateFile);
        StringWriter stringWriter = new StringWriter();
        template.process(dataMap,stringWriter);
        return stringWriter.toString();
    }


}
